﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Fire : MonoBehaviour {

    public Sprite weapen1;
    public Sprite weapen2;
	public PlayerInfo _playerInfo;
	public float nextFire;
	public float fireRate;
	private bool defauteWeapen;
	public float damegeWeapen1 = 1f;
	public float damegeWeapen2 = 3f;
	
	void Start () 
	{
		fireRate = 1;
		defauteWeapen = true;
	}
	
	// Update is called once per frame
	void Update () 
	{
        if(Input.GetKey(KeyCode.E))
        {
        	defauteWeapen = false;
        	fireRate = 0.2f;
            GetComponent<SpriteRenderer>().sprite = weapen2;
        }
        if(Input.GetKey(KeyCode.Q))
        {
        	defauteWeapen = true;
        	fireRate = 1f;
            GetComponent<SpriteRenderer>().sprite = weapen1;
        }

		nextFire += Time.fixedDeltaTime;
		if(Input.GetMouseButton(0) && nextFire>=fireRate)// && _playerInfo.bulletNumber>0
		{
			nextFire = 0;
			if(defauteWeapen)
				_playerInfo.bulletNumber--;
			Vector3 end = GameObject.Find("FirePoint").transform.position;
			Vector3 head = transform.position;

			RaycastHit2D hit = Physics2D.Raycast(end, end-head);//end-head
			//Debug.Log("Ray!!!");
			Health enemyHealth = hit.collider.GetComponent<Health>();
			if(enemyHealth != null)
			{
				//Debug.Log("Ray itit");
				if(defauteWeapen)
					enemyHealth.DesBlood(damegeWeapen1);
				else
					enemyHealth.DesBlood(damegeWeapen2);
			}
		}
	}
}
